// pages/identify/index/index.js
var QQMapWX = require('../../../untils/qqmap-wx-jssdk')
var qqmapsdk;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        place:[]
    },
    touchmove(){},
    getLocation1:function () {
        let that = this
        wx.getLocation({//获取地址
            type: 'gcj02',
            success(res) {
                const latitude = res.latitude
                const longitude = res.longitude
                const speed = res.speed
                const accuracy = res.accuracy
                that.setData({
                    latitude1:latitude,
                    longitude1:longitude
                })
                that.nearby_search()
                console.log(latitude, longitude, speed, accuracy)
                qqmapsdk.reverseGeocoder({//SDK调用
                    location: { latitude, longitude },
                    success: function (res) {
                        console.log(res)
                    }
                })
            }
        })
    },


      nearby_search:function(){
        var _this = this;

        // 调用接口
        qqmapsdk.search({
           keyword: '保险',  //搜索关键词
           location: _this.data.latitude1+","+_this.data.longitude1, //设置周边搜索中心点
           success: function (res) { //搜索成功后的回调
            console.log(res)
             var mks = []
             for (var i = 0; i < 3; i++) {
               mks.push({ // 获取返回结果，放到mks数组中
                 title: res.data[i].title,
                 id: res.data[i].id,
                 latitude: res.data[i].location.lat,
                 longitude: res.data[i].location.lng,
                 width: 20,
                 height: 20
               })
             }
             console.log(mks)
             _this.setData({ //设置markers属性，将搜索结果显示在地图中
               markers: mks
             })
           },
           fail: function (res) {
             console.log(res);
           },
           complete: function (res){
             console.log(res);
           }
       });
     },

    markerMap(e){
        let that = this
        let index = e.currentTarget.dataset.index
        this.setData({
            marker_to: [{
                id: 1,
                latitude: that.data.markers[index].latitude,
                longitude: that.data.markers[index].longitude,
                name: '....',
                iconPath: '../../../images/markers.png',
                width: "20",  
                height: "20" 
              }],
        })
        var _this = this;
        //调用距离计算接口
        qqmapsdk.direction({
        mode: 'driving',//可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
        //from参数不填默认当前地址
        from: _this.data.latitude1+","+_this.data.longitude1,
        to: _this.data.marker_to[0].latitude+","+_this.data.marker_to[0].longitude,
        success: function (res) {
            console.log(res);
            var ret = res;
            var coors = ret.result.routes[0].polyline, pl = [];
            //坐标解压（返回的点串坐标，通过前向差分进行压缩）
            var kr = 1000000;
            for (var i = 2; i < coors.length; i++) {
            coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
            }
            //将解压后的坐标放入点串数组pl中
            for (var i = 0; i < coors.length; i += 2) {
            pl.push({ latitude: coors[i], longitude: coors[i + 1] })
            }
            console.log(pl)
            //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
            _this.setData({
            latitude:pl[0].latitude,
            longitude:pl[0].longitude,
            polyline: [{
                points: pl,
                color: '#7C7CD3',
                width: 4
            }]
            })
        },
        fail: function (error) {
            console.error(error);
        },
        complete: function (res) {
            console.log(res);
        }
        });
    },
      
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        qqmapsdk = new QQMapWX({
            key: 'ERRBZ-U6GW2-R3WU5-CEEZY-KL7T5-3WBE6'
        });
        this.getLocation1()
        this.mapCtx = wx.createMapContext('myMap')
    },

    onReady(options) {
       
    }
})